package com.map.lbs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import com.map.lbs.http.ApiClient;
import com.map.lbs.http.PostDataSuccessEvent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class UserRegisterActivity extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_register);
		
		Button login = (Button) findViewById(R.id.btn_user_signup);
		Button register = (Button) findViewById(R.id.btn_user_register);
		
		login.setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View arg0) {
				EditText editMailAddr = (EditText) findViewById(R.id.user_register_mailaddress);
				EditText editPasswd = (EditText) findViewById(R.id.user_register_password);
				String mailAddress = editMailAddr.getText().toString();
				String password = editPasswd.getText().toString();
				
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("username", mailAddress);
				params.put("password", password);
				
				ApiClient client = new ApiClient("https://social-map.rhcloud.com/api/user/login");
				client.postData(params, new PostDataSuccessEvent(){

					@Override
					public void handleResponse(InputStream is) {
						InputStreamReader reader = new InputStreamReader(is);
						BufferedReader buffer = new BufferedReader(reader);
						try {
							String responseBody = buffer.readLine();
							Log.d(getPackageName(), responseBody);
							Intent intent = new Intent();
							intent.setClass(UserRegisterActivity.this, UserProfileActivity.class);
							
							startActivity(intent);
							UserRegisterActivity.this.finish();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
				}, null);
			}
			
		});
		
		register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//Validate
				EditText editMailAddr = (EditText) findViewById(R.id.user_register_mailaddress);
				EditText editPasswd = (EditText) findViewById(R.id.user_register_password);
				String mailAddress = editMailAddr.getText().toString();
				String password = editPasswd.getText().toString();
				
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("username", mailAddress);
				params.put("email", mailAddress);
				params.put("password", password);
				
				ApiClient client = new ApiClient("https://social-map.rhcloud.com/api/user/register");
				client.postData(params, new PostDataSuccessEvent(){

					@Override
					public void handleResponse(InputStream is) {
						InputStreamReader reader = new InputStreamReader(is);
						BufferedReader buffer = new BufferedReader(reader);
						try {
							String responseBody = buffer.readLine();
							Log.d(getPackageName(), responseBody);
							Intent intent = new Intent();
							intent.setClass(UserRegisterActivity.this, MapStageActivity.class);
							
							startActivity(intent);
							UserRegisterActivity.this.finish();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
				}, null);
				
			}
		});
	}
}